package com.colin.app.activity.view.picker.number;

import android.annotation.SuppressLint;
import android.widget.NumberPicker;

import com.colin.app.R;
import com.colin.app.activity.view.ViewContract;
import com.colin.app.activity.view.ViewPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.library.help.LogHelp;

public class NumberPickerActivity extends AppActivity implements ViewContract.ViewView, NumberPicker.Formatter, NumberPicker.OnValueChangeListener, NumberPicker.OnScrollListener {
    private ViewContract.ViewPresenter mViewPresenter = null;
    private NumberPicker picker_choose_number;
    private NumberPicker picker_choose_city;
    private MyNumberPicker picker_choose_list;
    private NumberPicker picker_choose_year;
    private NumberPicker picker_choose_month;
    private NumberPicker picker_choose_day;
    private NumberPicker picker_choose_hour;
    private NumberPicker picker_choose_minute;

    @Override
    protected void onDestroy() {
        if (null != mViewPresenter) {
            mViewPresenter.destroy();
            mViewPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mViewPresenter = new ViewPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_number_picker;
    }

    @Override
    public void initView() {
        this.picker_choose_number = (NumberPicker) this.findViewById(R.id.picker_choose_number);
        this.picker_choose_city = (NumberPicker) this.findViewById(R.id.picker_choose_city);
        this.picker_choose_list = (MyNumberPicker) this.findViewById(R.id.picker_choose_list);
        this.picker_choose_year = (NumberPicker) this.findViewById(R.id.picker_choose_year);
        this.picker_choose_month = (NumberPicker) this.findViewById(R.id.picker_choose_month);
        this.picker_choose_day = (NumberPicker) this.findViewById(R.id.picker_choose_day);
        this.picker_choose_hour = (NumberPicker) this.findViewById(R.id.picker_choose_hour);
        this.picker_choose_minute = (NumberPicker) this.findViewById(R.id.picker_choose_minute);
        String[] city = {"立水桥", "霍营", "回龙观", "龙泽", "西二旗", "上地"};
        picker_choose_city.setDisplayedValues(city);
        picker_choose_city.setMinValue(0);
        picker_choose_city.setMaxValue(city.length - 1);
        picker_choose_city.setValue(4);
        picker_choose_city.setWrapSelectorWheel(false);//禁止循环滚动 必须在最大值与最小值设置之后设置

        initNumberPickerView(picker_choose_number, 0, 99, 2);
        initNumberPickerView(picker_choose_year, 2017 - 30, 2017 + 50, 2017);
        initNumberPickerView(picker_choose_day, 1, 31, 2);
        initNumberPickerView(picker_choose_month, 1, 12, 2);
        initNumberPickerView(picker_choose_hour, 0, 24, 12);
        initNumberPickerView(picker_choose_minute, 0, 60, 0);

        picker_choose_list.setDisplayedValues(city);
        picker_choose_list.setMinValue(0);
        picker_choose_list.setMaxValue(city.length - 1);
        picker_choose_list.setValue(4);

    }


    private void initNumberPickerView(NumberPicker numberPicker, int min, int max, int value) {
        numberPicker.setMinValue(min);
        numberPicker.setMaxValue(max);
        numberPicker.setValue(value);
        numberPicker.setFormatter(this);
        numberPicker.setOnValueChangedListener(this);
        numberPicker.setOnScrollListener(this);
    }

    @Override
    public void initData() {
        mViewPresenter.setData();


    }


    @SuppressLint("DefaultLocale")
    @Override
    public String format(int value) {
        return String.format("%02d", value);
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        LogHelp.d("oldVal-->>" + String.valueOf(oldVal));
        LogHelp.d("newVal-->>" + String.valueOf(newVal));
    }

    @Override
    public void onScrollStateChange(NumberPicker view, int scrollState) {
        switch (scrollState) {
            case SCROLL_STATE_IDLE:
                toastShow("不滑动");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                toastShow("滑动中");
                break;
            case SCROLL_STATE_FLING:
                toastShow("后续滑动(飞呀飞，根本停下来)");
                break;

        }
    }
}
